package com.hjt.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hjt.gulimall.product.entity.AttrEntity;
import com.hjt.gulimall.product.service.AttrAttrgroupRelationService;
import com.hjt.gulimall.product.service.AttrService;
import com.hjt.gulimall.product.service.CategoryService;
import com.hjt.gulimall.product.vo.AttrGroupRelationVo;
import com.hjt.gulimall.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hjt.gulimall.product.entity.AttrGroupEntity;
import com.hjt.gulimall.product.service.AttrGroupService;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.R;


/**
 * 属性分组
 *
 * @author huangjingtai
 * @email 970075985@qq.com
 * @date 2021-09-08 16:15:19
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@PathVariable("catelogId") String catelogId, @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(catelogId, params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long[] cateLogPath =  categoryService.findCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(cateLogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取该分组的所有属性
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> entityList  = attrService.getRelationAttr(attrGroupId); // 获取改分组的所有属性
        return R.ok().put("data",entityList);
    }

    /**
     * 删除该分组的属性
     * @param params
     * @return
     */
    @PostMapping("attr/relation/delete")
    public R deleteAttr(@RequestBody List<AttrGroupRelationVo> params) {
        attrGroupService.deleteRelation(params);
        return R.ok();
    }

    /**
     * 获取没有被关联的属性
     * @param attrGroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getAttrNoRelation(@PathVariable String attrGroupId, @RequestParam Map<String, Object> params) {
        PageUtils page =  attrService.getNoRelationAttr(attrGroupId, params);

        return R.ok().put("page", page);
    }

    /**
     * 绑定属性 与 属性分组
     * @param relationVoList
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVoList) {
        relationService.saveBatch(relationVoList);
        return R.ok();

    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        // 1.查出当前分类下的所有分组
        // 2.查出每个属性分组关联的所有属性
        List<AttrGroupWithAttrVo> attrGroupWithAttrVo = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", attrGroupWithAttrVo);
    }

}
